package cn.edu.scau.dao;

import cn.edu.scau.entity.RecoveryTrack;
import cn.edu.scau.entity.WorkPlan;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Mapper
@CacheNamespace
@Repository
public interface WorkPlanMapper {
	/**
	 * 根据编号和日期删除工作计划
	 * @param workdate
	 * @param nurseid
	 * @return
	 */
    @Delete({
        "delete from workplan",
        "where workDate = #{workdate,jdbcType=DATE}",
          "and nurseID = #{nurseid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(@Param("workdate") Date workdate, @Param("nurseid") Integer nurseid);

    /**
     * 根据日期、护士编号、早午晚工作安排新增工作计划信息，不考虑null
     * @param record
     * @return
     */
    @Insert({
        "insert into workplan (workDate, nurseID, ",
        "workMorning, workAfternoon, ",
        "workEvening)",
        "values (#{workdate,jdbcType=DATE}, #{nurseid,jdbcType=INTEGER}, ",
        "#{workmorning,jdbcType=BIT}, #{workafternoon,jdbcType=BIT}, ",
        "#{workevening,jdbcType=BIT})"
    })
    int insert(WorkPlan record);

    /**
     * 根据日期、护士编号、早午晚工作安排新增工作计划信息,考虑null。若为空，不向数据库插入数据
     * @param record
     * @return
     */
    @InsertProvider(type=WorkPlanSqlProvider.class, method="insertSelective")
    int insertSelective(WorkPlan record);

    /**
     * 根据主键查询,不查询护士名
     * @param workdate
     * @param nurseid
     * @return
     */
    @Select({
        "select",
        "workDate, nurseID, workMorning, workAfternoon, workEvening",
        "from workplan",
        "where workDate = #{workdate,jdbcType=DATE}",
          "and nurseID = #{nurseid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="workDate", property="workDate", jdbcType=JdbcType.DATE, id=true),
        @Result(column="nurseID", property="nurseID", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="workMorning", property="workMorning", jdbcType=JdbcType.BIT),
        @Result(column="workAfternoon", property="workAfternoon", jdbcType=JdbcType.BIT),
        @Result(column="workEvening", property="workEvening", jdbcType=JdbcType.BIT)
    })
    WorkPlan selectByPrimaryKeyNoName(@Param("workdate") Date workdate, @Param("nurseid") Integer nurseid);
    
    /**
     * 根据主键查找工作计划，会查询护士名
     * @param workdate
     * @param nurseid
     * @return
     */
    @Select({
        "select",
        "workDate, nurseID, nurseName, workMorning, workAfternoon, workEvening",
        "from workplan natural join nurse",
        "where workDate = #{workdate,jdbcType=DATE}",
          "and nurseID = #{nurseid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="workDate", property="workDate", jdbcType=JdbcType.DATE, id=true),
        @Result(column="nurseID", property="nurseID", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="nurseName", property="nurseName", jdbcType=JdbcType.VARCHAR),
        @Result(column="workMorning", property="workMorning", jdbcType=JdbcType.BIT),
        @Result(column="workAfternoon", property="workAfternoon", jdbcType=JdbcType.BIT),
        @Result(column="workEvening", property="workEvening", jdbcType=JdbcType.BIT)
    })
    WorkPlan selectByPrimaryKey(@Param("workdate") Date workdate, @Param("nurseid") Integer nurseid);

    /**
     * 查询所有工作计划信息，不包括护士名
     * @return
     */
    @Select({
        "select",
        "workDate, nurseID, workMorning, workAfternoon, workEvening",
        "from workplan"
    })
    List<WorkPlan> selectAllNoName();
    
    /**
     * 查询所有工作计划信息，包括护士名
     * @return
     */
    @Select({
        "select",
        "workDate, nurseID, nurseName, workMorning, workAfternoon, workEvening",
        "from workplan natural join nurse"
    })
    List<WorkPlan> selectAll();
    
    /**
     * 查询所有工作计划信息，包括护士名，护士编号已确定
     * @param nurseid
     * @return
     */
    @Select({
        "select",
        "workDate, nurseID, nurseName, workMorning, workAfternoon, workEvening",
        "from workplan natural join nurse",
        "where nurseID = #{nurseid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="workDate", property="workDate", jdbcType=JdbcType.DATE, id=true),
        @Result(column="nurseID", property="nurseID", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="nurseName", property="nurseName", jdbcType=JdbcType.VARCHAR),
        @Result(column="workMorning", property="workMorning", jdbcType=JdbcType.BIT),
        @Result(column="workAfternoon", property="workAfternoon", jdbcType=JdbcType.BIT),
        @Result(column="workEvening", property="workEvening", jdbcType=JdbcType.BIT)
    })
    List<WorkPlan> selectWithNurseID(Integer nurseid);
    
    /**
     * 根据工作日期、护士编号、早午晚工作状态修改数据库,考虑null。若为空，不会更改数据库数据
     * @param record
     * @return
     */
    @UpdateProvider(type=WorkPlanSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(WorkPlan record);

    /**
     * 根据工作日期、护士编号、早午晚工作状态修改数据库,不考虑null。若为空，会将对应属性修改为空
     * @param record
     * @return
     */
    @Update({
        "update workplan",
        "set workMorning = #{workmorning,jdbcType=BIT},",
          "workAfternoon = #{workafternoon,jdbcType=BIT},",
          "workEvening = #{workevening,jdbcType=BIT}",
        "where workDate = #{workdate,jdbcType=DATE}",
          "and nurseID = #{nurseid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(WorkPlan record);
    
    /**
     * 根据工作日期、护士编号、护士名字组合查询
     * @param workDate
     * @param nurseID
     * @param nurseName
     * @return
     */
    @SelectProvider(type = WorkPlanSqlProvider.class,method = "find")
    List<WorkPlan> find(@Param("workDate") Date workDate,@Param("nurseID") Integer nurseID,@Param("nurseName") String nurseName);
    
    @SelectProvider(type = WorkPlanSqlProvider.class,method = "findWithNurseID")
    List<WorkPlan> findWithNurseID(@Param("workDate") Date workDate,@Param("nurseID") Integer nurseID,@Param("nurseName") String nurseName);
}